#ifndef __TRAIN__
#define __TRAIN__
#include <my_header.h>
#include <syslog.h>
typedef struct train_s{
    int flag;//新增加的
    int length;
    char data[1000];        
}train_t;

int recvn(int sockfd, void *buf, int length) {
    int total = 0;
    char *p = (char *)buf;
    while(total < length) {
        ssize_t sret = recv(sockfd, p+total, length-total, 0);
        total += sret;
    }
    return total;
}
// 打印日志
#define SYSLOG_PRINTF(type,message) \
    do{ \
        time_t now = time(NULL); \
        char *time_cur=ctime(&now);\
        size_t len=strlen(time_cur);\
        if(len>0&&time_cur[len-1]=='\n'){\
            time_cur[len-1]='\0';\
        }\
        syslog(type,"当前时间为:%s,当前操作为:%s\n",time_cur,message);\
    }while(0)\

int func_puts_client(int netfd,const char * filename)
{
    train_t *pTrain =(train_t *)calloc(1, sizeof(train_t));
    int ret = access(filename, F_OK);
    if (ret == -1) {
        printf("文件不存在!\n");
        return -1;
    }
    pTrain->length =strlen(filename);
    memcpy(pTrain->data, filename, pTrain->length);
    send(netfd, &pTrain->length, sizeof(pTrain->length), MSG_NOSIGNAL);
    send(netfd, pTrain->data, pTrain->length, MSG_NOSIGNAL);
    int fd = open(filename, O_RDWR);
    if (fd == -1) {
        printf("open file is failed!\n");
        return -1;
    }

    struct stat statbuf;
    fstat(fd, &statbuf);
    off_t filesize = statbuf.st_size;
    printf("filesize = %ld\n", filesize);
    pTrain->length = sizeof(filesize);
    memcpy(pTrain->data, &filesize, pTrain->length);
    send(netfd, &pTrain->length, sizeof(pTrain->length), MSG_NOSIGNAL);
    send(netfd, pTrain->data, pTrain->length, MSG_NOSIGNAL);

    while(1) {
        bzero(pTrain->data, sizeof(pTrain->data));
        ssize_t sret =read(fd, pTrain->data, sizeof(pTrain->data));
        pTrain->length = sret;
        send(netfd, &pTrain->length, sizeof(pTrain->length), MSG_NOSIGNAL);
        send(netfd, pTrain->data, pTrain->length, MSG_NOSIGNAL);
        if(sret == 0) {
            break;
        }

    }
    close(fd);
    return 0;
}
int func_gets_client(int netfd)
{
    sleep(1);
    train_t train;
    char filename[100]={0};
    recvn(netfd, &train.length, sizeof(train.length));
    recvn(netfd, train.data, train.length);
    memcpy(filename,train.data,train.length);
    int fd = open(filename, O_RDWR|O_CREAT|O_TRUNC, 0666);

    sleep(1);
    off_t filesize=0;
    recv(netfd,&filesize,sizeof(off_t),0);
    //recvn(netfd, &train.length, sizeof(train.length));
    //recvn(netfd, &filesize, train.length);
    printf("filesize = %ld\n", filesize);

    off_t cursize = 0;
    off_t lastsize = 0;
    off_t slice = filesize/10000;
    while(1) {
        if(cursize==filesize)break;
        recvn(netfd, &train.length, sizeof(train.length));
        if(train.length == 0) {
            break;
        }
        cursize += train.length;
        if(cursize - lastsize > slice) {
            printf("%5.2lf%%\r", 100.0*cursize/filesize);
            fflush(stdout);
            lastsize = cursize;
        }
        recvn(netfd, train.data, train.length);
        write(fd, train.data, train.length);
    }
    printf("success\n");
    //printf("100.00%%\n");
    close(fd);
    return 0;
}
#endif

